﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SLWebApp.ChildWindows
{
    public partial class ChildWindow_FlightVessels : ChildWindow
    {
        #region properties

        public string UserType
        {
            get
            {
                if (_parent.UserID == 1)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
        }


        #endregion

        #region cstor


        public ChildWindow_FlightVessels()
        {
            InitializeComponent();
        }

        public ChildWindow_FlightVessels(FlightsVessels parent)
            : this()
        {
            _parent = parent;
            _operationType = OperationType.Add;
            InitData();
        }

        public ChildWindow_FlightVessels(FlightsVessels parent, ServiceReference_HB.WEB_VM_LINERS model)
        {
            _parent = parent;
            _operationType = OperationType.Update;
            InitData();
            DataContext = model;
        }



        #endregion

        #region private methods

        private void InitData()
        {
            string userType = _parent.UserID == 1 ? "1" : "0";
            clientService.GetMainSeaRoutes_ByUserIdAsync(_parent.UserID, userType);

            clientService.GetMainSeaRoutes_ByUserIdCompleted += (s, e) =>
                {
                    ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
                    cbmSL_first.ItemsSource = col;
                };


            //绑定船名
            clientService.GetAgentIDByUserIDAsync(_parent.UserID);
            clientService.GetAgentIDByUserIDCompleted += (s, e) =>
                {
                    string agentID = e.Result;
                    string strWhere = string.Empty;

                    if (this.UserType == "1")
                    {

                    }
                    else
                    {
                        strWhere = string.Format("(VSL_CST_VCM_ID = {0} or VSL_CST_AGT_ID = {0})", agentID);
                    }

                    clientService.GetAllVesselsAsync(strWhere, " VSL_ENNAME ");
                    clientService.GetAllVesselsCompleted += (s2, e2) =>
                        {
                            ObservableCollection<ServiceReference_HB.ST_Vessels> items = e2.Result;
                            cmbEngName.ItemsSource = items;
                        };

                };



        }


        private decimal? ParseStrToDecimal(string str)
        {
            decimal value;
            if (string.IsNullOrEmpty(str))
                return null;
            bool check = decimal.TryParse(str, out value);
            if (check)
                return value;
            else
                return null;
        }
        #endregion

        #region fields

        private FlightsVessels _parent;

        private ServiceReference_HB.ServiceForSLClient clientService = new ServiceReference_HB.ServiceForSLClient();

        private OperationType _operationType;

        #endregion


        #region event handler


        private void cmbFirstTer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {

                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;


                clientService.GetChildSeaRoutes_ByLineIDAsync(_parent.UserID, UserType, item.ID);

                clientService.GetChildSeaRoutes_ByLineIDCompleted += (s, e1) =>
                    {
                        cmbSL_second.ItemsSource = e1.Result;
                        cmbSL_second.SelectedValuePath = "ID";
                    };
                //获取班期

            }

        }

        private void cmbSL_second_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {

                ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;
                //班期
                clientService.GetVesselDateAsync(_parent.UserID, item.ID.ToString());
                clientService.GetVesselDateCompleted += (s, e1) =>
                    {
                        ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e1.Result;
                        comboxVesselDate.ItemsSource = col;
                    };

            }
        }

        private void cmbEngName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ServiceReference_HB.ST_Vessels item = e.AddedItems[0] as ServiceReference_HB.ST_Vessels;
                txtBoxCNName.Text = item.VSL_CNNAME;
            }
        }

        #endregion

        private void btnSave_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            progressbar.IsBusy = true;
            ServiceReference_HB.WEB_VM_LINERS model = new ServiceReference_HB.WEB_VM_LINERS();

            ServiceReference_HB.MainSeaRoutes seaRout = cmbSL_second.SelectedItem as ServiceReference_HB.MainSeaRoutes;
            ServiceReference_HB.ST_Vessels vessels = cmbEngName.SelectedItem as ServiceReference_HB.ST_Vessels;
            ServiceReference_HB.MainSeaRoutes date = comboxVesselDate.SelectedItem as ServiceReference_HB.MainSeaRoutes;

            
           
            if (vessels != null)
            {
                model.LNR_VSL_CD = vessels.VSL_CD;
                model.LNR_VSL_ID = vessels.VSL_ID;
            }
            if (date != null)
            {
                model.LNR_FLT_ID = date.ID;
            }
           // model.LNR_NO = date.ID;

            model.LNR_ISTEP = ParseStrToDecimal(txtLNR_ISTEP.Text);
            model.LNR_ESTEP = ParseStrToDecimal(txtLNR_ESTEP.Text);
            model.LNR_IVOYAGE = txtLNR_IVOYAGE.Text;
            model.LNR_EVOYAGE = txtLNR_EVOYAGE.Text;
            model.LNR_IAMOUNT = ParseStrToDecimal(txtLNR_IAMOUNT.Text);
            model.LNR_EAMOUNT = ParseStrToDecimal(txtLNR_EAMOUNT.Text);


            clientService.IUDForVesselAsync((int)OperationType.Add, _parent.UserID, model);
           
            clientService.IUDForVesselCompleted += (s1, e1) =>
                {
                    string result = e1.Result;
                    if (result[0] == '1')
                    {
                        this.Close();
                        _parent.InitDate();
                    }
                    else
                    {
                        MessageBox.Show(result.Substring(3));
                        progressbar.IsBusy = false;
                    }

                };

        }


    }



}